<template>
  <div id="space-usage-analyze">
    <a-flex gap="middle">
      <a-card title="存储空间额度" style="width: 50%">
        <div style="height: 320px; text-align: center">
          <a-tooltip :title="formatSize(data.usedSize) + ' / ' + formatSize(data.maxSize)" color="#87d068">
            <a-progress
              type="dashboard"
              :percent="data.sizeUsageRatio ?? 0"
              :stroke-color="{
                '0%': '#e99210',
                '100%': '#ef4e06',
              }"
              strokeWidth="10"
              :size="200"
            />
          </a-tooltip>
        </div>
      </a-card>
      <a-card title="图片数量额度" style="width: 50%">
        <div style="height: 320px; text-align: center">
          <a-tooltip :title="data.usedCount + ' / ' + data.maxCount" color="#108ee9">
            <a-progress
              type="dashboard"
              :percent="data.sizeUsageRatio ?? 0"
              :stroke-color="{
                '0%': '#108ee9',
                '100%': '#87d068',
              }"
              strokeWidth="10"
              :size="200"
            />
          </a-tooltip>
        </div>
      </a-card>
    </a-flex>
  </div>
</template>

<script lang="ts" setup>
import { ref, watchEffect } from 'vue'
import { getSpaceUsageAnalyzeUsingPost } from '@/api/spaceAnalyzeController'
import { message } from 'ant-design-vue'
import { formatSize } from '../../utils'

interface Props {
  queryAll?: boolean
  queryPublic?: boolean
  spaceId?: number
}

const props = withDefaults(defineProps<Props>(), {
  // 默认为false，这样如果单独分析某空间只需要传spaceId
  queryAll: false,
  queryPublic: false,
})

// 接受图表数据
const data = ref<API.SpaceUsageAnalyzeResponse>({})
const loading = ref<boolean>(true) // 默认为true，加载

/**
 * 加载数据
 */
const fetchData = async () => {
  loading.value = true
  const res = await getSpaceUsageAnalyzeUsingPost({
    queryAll: props.queryAll,
    queryPublic: props.queryPublic,
    spaceId: props.spaceId,
  })
  if (res.data.code === 0 && res.data.data) {
    data.value = res.data.data
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
  // 加载完毕结束加载状态
  loading.value = false
}

/**
 * 监听变量，改变时触发数据的重新加载
 * 只要有任何一个值发生变化，都会重新执行封装的函数
 */
watchEffect(() => {
  fetchData()
})
</script>

<style scoped></style>
